package jp.co.mclnet.sample;

public class Sample4JDBC {

    public static void main(String[] args) {
        String server = "localhost";
        String[] sql;
        sql = new String[] {
                "INSERT INTO first VALUES (null,now()),(null,now()),(null,now()),(null,now()),(null,now()),(null,now()),(null,now()),(null,now()),(null,now()),(null,now())",
                "INSERT INTO second SELECT null,val2_date FROM first",
                "INSERT INTO first  SELECT null,val2_date FROM second" };
        sql = new String[] {
                "DROP TABLE IF EXISTS first",
                "CREATE TABLE first (key1_int INT auto_increment,val2_date datetime,PRIMARY KEY(key1_int))",
                "DROP TABLE IF EXISTS second", "CREATE TABLE second LIKE first" };

        java.sql.Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://" + server + ":3306/test";
            conn = java.sql.DriverManager.getConnection(url, "root", "");

            for (String s : sql) {
                java.sql.PreparedStatement pst = conn.prepareStatement(s);
                pst.executeUpdate();
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (java.sql.SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (java.sql.SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
